package com.example.softwareengg;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.text.SpannableString;
import android.text.style.UnderlineSpan;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Screenin extends HeaderFooter implements OnClickListener {

	EditText et;
	Button b;
	String serverIP = MyOverridenApp.getserverIP();
	String fileDesc;
	int flag = 0;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ViewGroup vg = (ViewGroup) findViewById(R.id.lldata);
        ViewGroup.inflate(Screenin.this, R.layout.screenin, vg);
		
        TextView userName1 = (TextView)findViewById(R.id.textUsername);
        
		String shruti2 ="Welcome " + MyOverridenApp.getUser();
	
		
		
		et=(EditText)findViewById(R.id.editText2);
		b=(Button)findViewById(R.id.button1);
		
		Button clear = (Button)findViewById(R.id.button2);
		TextView ReqHead = (TextView) findViewById(R.id.reqhead);
		SpannableString content1 = new SpannableString("Request");
		SpannableString content2 = new SpannableString(shruti2);
		content1.setSpan(new UnderlineSpan(), 0, content1.length(), 0);
		content2.setSpan(new UnderlineSpan(), 0, content2.length(), 0);
		userName1.setText(content2);
		ReqHead.setText(content1);
		
		
		b.setOnClickListener(this);
		clear.setOnClickListener(this);
		Button button_logout = (Button) findViewById(R.id.Button_logout);
		Button home = (Button)findViewById(R.id.home);
	
		button_logout.setOnClickListener(this);
		home.setOnClickListener(this);
		
		
	}
	
		
			@Override
			public void onClick(View v) {
				 switch (v.getId()) {
				 
				 case R.id.home:
			        {
						   Intent intent = new Intent(Screenin.this,ScreenB.class);
						    startActivity(intent);
						    break;

			        }
				 case R.id.Button_logout:
			        {
			        	try{
			        		Toast.makeText(getApplicationContext(),"Logout", Toast.LENGTH_LONG).show();
			        		
			        	Socket client = MyOverridenApp.getClient();
			        	DataOutputStream outToServer = new DataOutputStream(client.getOutputStream());
						
						outToServer.writeBytes("signout\n");
						client.close();
						}
			        	catch(Exception e){
			        		e.printStackTrace();
			        		Toast.makeText(getApplicationContext(),"Error Logging out", Toast.LENGTH_LONG).show();
			        		
			        	}
						   Intent intent = new Intent(Screenin.this,ScreenA.class);
						    startActivity(intent);
						    break;

			        }

				// TODO Auto-generated method stub
			        case R.id.button1:
			        {
			        	//request
			        	Socket client;
						try {
							client = MyOverridenApp.getClient();
							DataOutputStream outToServer = new DataOutputStream(client.getOutputStream());
							fileDesc = et.getText().toString();
				        	 outToServer.writeBytes("filedesc "+fileDesc+"\n");
				        	 
				        	// BufferedReader inFromServer = new BufferedReader(new InputStreamReader(client.getInputStream()));
								//String serverReply = inFromServer.readLine();
								//if(serverReply.equalsIgnoreCase("true"))
									flag = 1;
				        	
								
								
									Toast.makeText(getApplicationContext(),"File Desc sent", Toast.LENGTH_LONG).show();
									 Intent intent1 = new Intent(/*FirstActivity.this*/ v.getContext(), ScreenB.class);
									    startActivity(intent1);
								
						} catch (UnknownHostException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						 break;
			        	
				 }
				
			
			        case R.id.button2:
			        	EditText des = (EditText)findViewById(R.id.editText2);
			        	des.setText("");
			        	    	break;
					default:
						break;
					
				
				
			}
				 
		}
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
	
	@Override
	public void onBackPressed() {
	}

}
